﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using wrsm.wow;
using wrsm.wow.entity;
using wrsm.wow.exceptions;

namespace wrsm.client {

    /// <summary>
    /// A simple UI to stick on top of Wow Installation Detection
    /// </summary>
    public partial class WowInstallationDetectForm : Form {

        #region [Local Attributes]

        /// <summary>
        /// World of Warcraft Installation Utility
        /// </summary>
        private WowInstallationUtility _wowInstallUtility;

        /// <summary>
        /// World of Warcraft Installation Manager
        /// </summary>
        private WowInstallationManager _wowInstallManager;

        #endregion

        #region [Construction]

        /// <summary>
        /// Initialize Wow Installation Detect Form
        /// </summary>
        public WowInstallationDetectForm() {
            InitializeComponent();
        }

        /// <summary>
        /// Initialize Wow Installation Detect Form
        /// </summary>
        /// <param name="utility">Wow Installation Utility</param>
        /// <param name="manager">Wow Installation Manager</param>
        internal WowInstallationDetectForm(WowInstallationUtility utility, WowInstallationManager manager) : this() {
            _wowInstallUtility = utility;
            _wowInstallManager = manager;
        }

        #endregion

        #region [Form Events]

        /// <summary>
        /// Fires when form loads
        /// </summary>
        private void OnFormLoad(object sender, EventArgs e) {
            if (_wowInstallUtility == null || _wowInstallManager == null) {
                try {
                    WowInstallation install = _wowInstallUtility.LocateWowInstallationDirectory();
                    _wowInstallManager.AddInstall(install);
                } catch (WowApplicationException) {
                    DialogResult result = MessageBox.Show("Unable to locate a valid World of Warcraft Installation.  This process uses typical install locations to determine, if you know where you're installation is located, Would you like to select your installation location?.",
                        "Error Detetecting Installation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

                    //If the user is not interested in correcting this
                    if (result == System.Windows.Forms.DialogResult.No) {
                        Close();
                    } else {
                        //The user is interested in correcting this.
                        if (folderBrowser.ShowDialog(this) == System.Windows.Forms.DialogResult.OK
                            && !string.IsNullOrEmpty(folderBrowser.SelectedPath)) {
                                WowInstallation install = _wowInstallUtility.LoadInstallationDirectory(folderBrowser.SelectedPath);
                                _wowInstallManager.AddInstall(install);
                                Close();
                        }
                    }
                }
                Close();
            }
        }

        #endregion

    }
}
